package com.chonor.mall.service.impl;

import com.chonor.mall.dao.RoleDao;
import com.chonor.mall.mapper.RoleMapper;
import com.chonor.mall.mapper.RoleMenuMapper;
import com.chonor.mall.mapper.RoleResourceMapper;
import com.chonor.mall.model.*;
import com.chonor.mall.service.AdminCacheService;
import com.chonor.mall.service.RoleService;
import com.chonor.mall.vo.RoleDetailVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private AdminCacheService adminCacheService;

    @Override
    public int create(Role role) {
        role.setCreateat(new Date());
        role.setStatus(1);
        role.setAdminCount(0L);
        return roleMapper.insert(role);
    }

    @Override
    public int update(Long id, Role role) {
        role.setId(id);
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    @Override
    public RoleDetailVO detail(Long id) {
        RoleDetailVO roleDetailVO = new RoleDetailVO();

        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(id);
        Role role = roleMapper.selectByPrimaryKey(id);
        List<Menu> menus = listMenu(id);
        List<Resource> resources = listResource(id);

        roleDetailVO.setRole(role);
        roleDetailVO.setMenus(menus);
        roleDetailVO.setResources(resources);

        return roleDetailVO;
    }

    @Override
    public int delete(List<Long> ids) {
        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(ids);
        int count = roleMapper.deleteByExample(example);
        adminCacheService.delResourceListByRoleIds(ids);
        return count;
    }

    @Override
    public int deleteById(Long id) {
        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(id);
        int count = roleMapper.deleteByExample(example);
        adminCacheService.delResourceListByRole(id);
        return count;
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectByExample(new RoleExample());
    }

    @Override
    public List<Role> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        RoleExample example = new RoleExample();
        if (!StringUtils.isEmpty(keyword)) {
            example.createCriteria().andNameLike("%" + keyword + "%");
        }
        return roleMapper.selectByExample(example);
    }

    @Override
    public List<Menu> getMenuList(Long adminId) {
        return roleDao.getMenuList(adminId);
    }

    @Override
    public List<Menu> listMenu(Long roleId) {
        return roleDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<Resource> listResource(Long roleId) {
        return roleDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        RoleMenuExample example=new RoleMenuExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleMenuMapper.deleteByExample(example);
        //批量插入新关系
        for (Long menuId : menuIds) {
            RoleMenu relation = new RoleMenu();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuMapper.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        RoleResourceExample example=new RoleResourceExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        roleResourceMapper.deleteByExample(example);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            RoleResource relation = new RoleResource();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceMapper.insert(relation);
        }
        adminCacheService.delResourceListByRole(roleId);
        return resourceIds.size();
    }
}
